<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BaseController extends Controller
{
    protected $user_id = 0;
    protected $time = 0;

    public function __construct()
    {
        $this->user_id = request('user_id',0);
        $this->time = time();
    }

    /**
     * 接口返回数据  自定义
     * @param array $params
     * @return \Illuminate\Http\JsonResponse
     */
    public function res(array $params = [])
    {
        $data = array(
            'code'      => isset($params['code']) ? $params['code'] : 0,
            'message'   => isset($params['message']) ? $params['message'] : 'success',
            'data'      => !empty($params['data']) ? $params['data'] : (object)array(),
        );

        return response()->json($data);
    }

    public function success(array $data = [], $message = 'ok')
    {
        if (is_array($data) && empty($data))
        {
            $data = (object)array();
        }
        return $this->res(['message'=>$message, 'data'=>$data]);
    }

    public function error($code, $message = '')
    {
        return $this->res(['code'=>$code, 'message'=>$message]);
    }
}
